home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / gen / SkipBag.hP < prev    next >
Text File  |  1993-11-18  |  4KB  |  172 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1991 Free Software Foundation
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. /*
  19.  * Bags implemented via William Pugh SkipList algorithms.
  20.  * CACM, June 1990, p 668-676.
  21.  *
  22.  */
  23.  
  24. #ifndef _<T>SkipBag_h
  25. #ifdef __GNUG__
  26. #pragma interface
  27. #endif
  28. #define _<T>SkipBag_h 1
  29.  
  30. #include "<T>.Bag.h"
  31.  
  32. #include <limits.h>
  33. #include <MLCG.h>
  34.  
  35. class <T>SkipBag;
  36. class <T>RealSkipBagNode;
  37.  
  38. class <T>SkipBagNode
  39. {
  40. friend class <T>SkipBag;
  41.   private:
  42.     <T>RealSkipBagNode * * forward;
  43.     <T>SkipBagNode(int size);
  44. };
  45.  
  46. class <T>RealSkipBagNode : public <T>SkipBagNode
  47. {
  48. friend class <T>SkipBag;
  49.   private:
  50.     <T>             item;
  51.     <T>RealSkipBagNode(<T&> h, int size);
  52. };
  53.  
  54. typedef <T>RealSkipBagNode* <T>SkipBagNodePtr;
  55.  
  56. inline <T>SkipBagNode::<T>SkipBagNode(int size)
  57. : forward(new <T>SkipBagNodePtr[size+1])
  58. {
  59. }
  60.  
  61. inline <T>RealSkipBagNode::<T>RealSkipBagNode(<T&> h, int size)
  62. : item(h),
  63.   <T>SkipBagNode(size)
  64. {
  65. }
  66.  
  67. class <T>SkipBag : public <T>Bag
  68. {
  69. friend class <T>SkipBaginit;
  70.   protected:
  71.     <T>SkipBagNode*   header;
  72.     int               level;
  73.     int               max_levels;
  74.     int               randoms_left;
  75.     long              random_bits;
  76.     
  77.     static MLCG*      gen;
  78.     int               random_level(void);
  79.     
  80.     <T>SkipBagNodePtr leftmost(void);
  81.     <T>SkipBagNodePtr rightmost(void);
  82.     <T>SkipBagNodePtr pred(<T>SkipBagNodePtr t);
  83.     <T>SkipBagNodePtr succ(<T>SkipBagNodePtr t);
  84.     void              _kill(void);
  85.     
  86.   private:
  87.     enum { BITS_IN_RANDOM = LONGBITS-1 };
  88.     
  89.   public:
  90.     <T>SkipBag(long size=DEFAULT_INITIAL_CAPACITY);
  91.     <T>SkipBag(<T>SkipBag& a);
  92.     ~<T>SkipBag(void);
  93.     
  94.     Pix           add(<T&> i);
  95.     void          del(<T&> i);
  96.     void          remove(<T&>i);
  97.     int           nof(<T&> i);
  98.     int           contains(<T&> i);
  99.     
  100.     void          clear(void);
  101.     
  102.     Pix           first(void);
  103.     void          next(Pix& i);
  104.     <T>&          operator () (Pix i);
  105.     Pix           seek(<T&> i, Pix from = 0);
  106.     
  107.     Pix           last(void);
  108.     void          prev(Pix& i);
  109.     
  110.     int           OK(void);
  111. };
  112.  
  113. inline <T>SkipBagNodePtr <T>SkipBag::leftmost(void)
  114. {
  115.     return header->forward[0];
  116. }
  117.  
  118. inline <T>SkipBagNodePtr <T>SkipBag::succ(<T>SkipBagNodePtr t)
  119. {
  120.     <T>SkipBagNodePtr result = 0;
  121.     if (t->forward[0]!=header) result = t->forward[0];
  122.     return result;
  123. }
  124.  
  125. inline Pix <T>SkipBag::first(void)
  126. {
  127.     return Pix(leftmost());
  128. }
  129.  
  130. inline Pix <T>SkipBag::last(void)
  131. {
  132.     return Pix(rightmost());
  133. }
  134.  
  135. inline void <T>SkipBag::next(Pix& i)
  136. {
  137.     if (i != 0) i = Pix(succ((<T>SkipBagNodePtr)i));
  138. }
  139.  
  140. inline <T>& <T>SkipBag::operator () (Pix i)
  141. {
  142.     if (i == 0) error("null Pix");
  143.     return ((<T>SkipBagNodePtr)i)->item;
  144. }
  145.  
  146. inline void <T>SkipBag::prev(Pix& i)
  147. {
  148.     if (i != 0) i = Pix(pred((<T>SkipBagNodePtr)i));
  149. }
  150.  
  151. inline int <T>SkipBag::contains(<T&> key)
  152. {
  153.     return seek(key) != 0;
  154. }
  155.  
  156. inline <T>SkipBag::~<T>SkipBag()
  157. {
  158.     _kill();
  159.     delete header;
  160. }
  161.  
  162. static class <T>SkipBaginit
  163. {
  164.   public:
  165.     <T>SkipBaginit();
  166.     ~<T>SkipBaginit();
  167.   private:
  168.     static int count;
  169. } <T>skipBaginit;
  170.  
  171. #endif
  172.